//
//  XWImagePickerSheet.m
//  HappyTravel
//
//  Created by JiePan’s Mac on 2018/8/6.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "XWImagePickerSheet.h"

@interface XWImagePickerSheet () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@end

@implementation XWImagePickerSheet

#pragma mark - init
- (instancetype)init {
    self = [super init];
    if (self) {
        if (!_arrSelected) {
            self.arrSelected = [NSMutableArray array];
        }
    }
    return self;
}
//显示选择照片提示Sheet
- (void)showImgPickerActionSheetInView:(UIViewController *)controller {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"Choose photos" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *actionCamera = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"Take photos"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (!self->imaPic) {
            self->imaPic = [[UIImagePickerController alloc] init];
        }
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self->imaPic.sourceType = UIImagePickerControllerSourceTypeCamera;
            self->imaPic.delegate = self;
            [self->viewController presentViewController:self->imaPic animated:NO completion:nil];
        }

    }];
    UIAlertAction *actionAlbum = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"Select from photo albums"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self loadImgDataAndShowAllGroup];
    }];
    [alertController addAction:actionCancel];
    [alertController addAction:actionCamera];
    [alertController addAction:actionAlbum];
    viewController = controller;
    [viewController presentViewController:alertController animated:YES completion:nil];

}
#pragma mark - 加载照片数据
- (void)loadImgDataAndShowAllGroup {
    if (!_arrSelected) {
        self.arrSelected = [NSMutableArray array];
    }
    [[MImaLibTool shareMImaLibTool] getAllGroupWithArrObj:^(NSArray *arrObj) {
        if (arrObj && arrObj.count > 0) {
            self.arrGroup = arrObj;
            if ( self.arrGroup.count > 0) {
                MShowAllGroup *svc = [[MShowAllGroup alloc] initWithArrGroup:self.arrGroup arrSelected:self.arrSelected];
                svc.delegate = self;
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:svc];
                if (self->_arrSelected) {
                    svc.arrSeleted = self->_arrSelected;
                    svc.mvc.arrSelected = self->_arrSelected;
                }
                svc.maxCout = self->_maxCount;
                [self->viewController presentViewController:nav animated:YES completion:nil];
            }
        }
    }];
}
#pragma mark - 拍照获得数据
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *theImage = nil;
    // 判断，图片是否允许修改
    if ([picker allowsEditing]){
        //获取用户编辑之后的图像
        theImage = [info objectForKey:UIImagePickerControllerEditedImage];
    } else {
        // 照片的元数据参数
        theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    if (theImage) {
        //保存图片到相册中
        MImaLibTool *imgLibTool = [MImaLibTool shareMImaLibTool];
        [imgLibTool.lib writeImageToSavedPhotosAlbum:[theImage CGImage] orientation:(ALAssetOrientation)[theImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
            if (error) {
            } else {
                
                //获取图片路径
                [imgLibTool.lib assetForURL:assetURL resultBlock:^(ALAsset *asset) {
                    if (asset) {
                        
                        [self->_arrSelected addObject:asset];
                        [self finishSelectImg];
                        [picker dismissViewControllerAnimated:NO completion:nil];
                    }
                } failureBlock:^(NSError *error) {
                    
                }];
            }
        }];
    }

}

#pragma mark - 完成选择后返回的图片Array(ALAsset*)
- (void)finishSelectImg {
    //正方形缩略图
    NSMutableArray *thumbnailImgArr = [NSMutableArray array];
    
    for (ALAsset *set in _arrSelected) {
        CGImageRef cgImg = [set thumbnail];
        UIImage* image = [UIImage imageWithCGImage: cgImg];
        [thumbnailImgArr addObject:image];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(getSelectImageWithALAssetArray:thumbnailImageArray:)]) {        
        [self.delegate getSelectImageWithALAssetArray:_arrSelected thumbnailImageArray:thumbnailImgArr];
    }
}





@end
